﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using OgilvyOne.Model;
using OgilvyOne.BLL;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
using ECCIT.Web;
using System.Collections.Generic;
public partial class AudEdit : AdminBasePage
{

    
    int langid = 0;
    AudioBLL audbll = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            langid = Convert.ToInt32(Request.QueryString["langid"]);
            audbll = new AudioBLL(langid);
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params["layerid"]);
            if (Request.QueryString["id"]!=null)
            {
                databind( Convert.ToInt32( Request.QueryString["id"]));
            }
        }
    }
    private bool add(Audio aud)
    {
        langid = Convert.ToInt32(Request.QueryString["langid"]);
        audbll = new AudioBLL(langid);
        return audbll.add(aud);
       
    }
    private bool update( Audio aud)
    {
        langid = Convert.ToInt32(Request.QueryString["langid"]);
        audbll = new AudioBLL(langid);
        return audbll.update(aud);
        
    }
    private void databind(int id)
    {
     Audio aud= audbll.getmodByid(id);
     title.Text = aud.Name;
     isedit.Value = aud.Id.ToString();
     txtAuthor.Text = aud.Author;
     txtOrder.Text = aud.Order.ToString();
     txtDesc.Text = aud.Description;
     indextop.SelectedIndex= aud.Release?1:0;
     
    }
    private Audio tomod()
    {
        Audio aud=null;
        if (isedit.Value == "$")
        {
            aud = new Audio(langid);
            aud.Uploaddatatime = DateTime.Now;
            aud.Lang_id =Convert.ToInt32( Request.QueryString["langid"]);
        }
        else
        {
            audbll = new AudioBLL(langid);
            aud = audbll.getmodByid(Convert.ToInt32(isedit.Value));
            if (audFile.HasFile)
            {
                aud.Uploaddatatime = DateTime.Now;
            }
        }
       
        aud.Column_id = Convert.ToInt32(Request.QueryString["columnid"]);
        aud.Layerid = Request.QueryString["layerid"];
        aud.Name = title.Text;
        aud.Author = txtAuthor.Text;
        aud.Description = txtDesc.Text;
        try
        {
            aud.Order = Convert.ToInt32(txtOrder.Text);
        }
        catch (Exception)
        {
            
           
        }

             if (audFile.HasFile)
            {

            aud.FilePath = saveFile(audFile, "aud");
            }
        
            if (upBigImg.HasFile)
            {
                aud.Bigimg = saveFile(upBigImg,"bigimg");
            }
            if (upThumbimg.HasFile)
            {
                aud.Thumbimg = saveFile(upThumbimg,"thumbimg");
            }
            aud.Release = Convert.ToBoolean(indextop.SelectedValue);
       

        return aud;
    }
    private string saveFile(FileUpload file,string mediaType)
    {


        string path = Guid.NewGuid().ToString() + file.FileName.Substring(file.FileName.LastIndexOf("."));
        string mpath = "";
        if (mediaType == "aud")
        {
            mpath = ConfigurationManager.AppSettings["audpath"];
        }
        else
        {
            mpath = ConfigurationManager.AppSettings["audimgpath"];
        }
        string vpath = String.Format("{0}{1}",mpath , path);
        file.SaveAs( Server.MapPath( vpath));
        return path;
        //ECCIT.IO.DataItemPath dp=new ECCIT.IO.DataItemPath ( mpath,ECCIT.IO.PathType.VirtualPath);
        
        //UploadedFile.Save(file, dp, null);
        


       
    }



    protected void Button1_Click(object sender, EventArgs e)
    {

        if (isedit.Value == "$")
        {
            if (add(tomod()))
            {
                Response.Redirect("AudioContent.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"] + "&langid=" + Request.Params["langid"], true);
            }

        }
        else
        {
            if (update(tomod()))
            {
                Response.Redirect("AudioContent.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"] + "&langid=" + Request.Params["langid"], true);
            } 
        }
            

    }
}
